package com.zzx.schoolfellow.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzx.schoolfellow.common.api.ApiResult;
import com.zzx.schoolfellow.mapper.BmsBillboardMapper;
import com.zzx.schoolfellow.model.entity.BmsBillboard;
import com.zzx.schoolfellow.model.entity.UmsUser;
import com.zzx.schoolfellow.service.IBmsBillboardService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @program: schoolfeoolw_backend
 * @description: 公告板控制层类
 * @author: zzx
 * @create: 2023-03-07 16:49
 **/
@RestController //自动返回的java对象转换成json字符串
@RequestMapping("/billboard") //指定路径请求
public class BmsBillboardController extends BaseController {

    @Resource //自动找到实现类并创建出来
    private IBmsBillboardService bmsBillboardService;
    @Resource
    private BmsBillboardMapper bmsBillboardMapper;

    @GetMapping("/show") //查询处理GET请求,二层路径
    public ApiResult<BmsBillboard> getNotices() {
        List<BmsBillboard> list = bmsBillboardService.list(
                new LambdaQueryWrapper<BmsBillboard>()  //lambda查询包装器
                        .eq(BmsBillboard::isShow, true)  //eq -》等于 指定查询条件
        ); //查询方法
        return ApiResult.success(list.get(list.size() - 1));
    }


    @GetMapping("/list")
    public List<BmsBillboard> getUserList(@RequestParam("keyword") String keyword) {
        List<BmsBillboard> lists;
        if (Objects.equals(keyword, "")) {
            lists = bmsBillboardService.list();
        } else {
            lists = bmsBillboardService.list(new QueryWrapper<BmsBillboard>()
                    .like("content", keyword));
        }
        return lists;
//        return bmsBillboardService.list();
    }

    @DeleteMapping("/delete/{id}")
    public ApiResult<String> deleteTag(@PathVariable("id") String id) {
        bmsBillboardService.removeById(id);
        return ApiResult.success(null, "删除成功");
    }

    @PostMapping("/update")
    public ApiResult<BmsBillboard> updatePromote(@Valid @RequestBody BmsBillboard name) {
        bmsBillboardService.updateById(name);
        return ApiResult.success(name);
    }

    @PostMapping("/add")
    public ApiResult<BmsBillboard> add(@Valid @RequestBody BmsBillboard name) {
        name.setCreateTime(yourMethod());
        bmsBillboardMapper.insert(name);
        return ApiResult.success(name);
    }

    public Date yourMethod() {
        LocalDateTime now = LocalDateTime.now();
        return Date.from(now.atZone(ZoneId.systemDefault()).toInstant());
    }
}
